package cn.jbolt.core.poi.excel;

import cn.jbolt.core.util.JBoltDateUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

public class JBoltWaterMarkUtil {
	public static BufferedImage createJBoltWatermarkImage(JBoltWaterMark watermark) {
		String[] textArray = watermark.getText().split("\n");
		int width = watermark.getWidth();
		int height = watermark.getHeight();

		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 背景透明 开始
		Graphics2D g = image.createGraphics();
		image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		g.dispose();
		// 背景透明 结束
		g = image.createGraphics();
		g.setColor(new Color(Integer.parseInt(watermark.getColor().substring(1), 16)));// 设定画笔颜色
		g.setFont(watermark.getFont());// 设置画笔字体
		g.shear(0.1, -0.26);// 设定倾斜度

		// 设置字体平滑
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int y = watermark.getHeight() / 2;
		for (int i = 0; i < textArray.length; i++) {
			g.drawString(textArray[i], 0, y);// 画出字符串
			y = y + watermark.getFont().getSize();
		}
		if (watermark.getDate() != null) {
			g.drawString(JBoltDateUtil.format(watermark.getDate(), watermark.getDateFormat()), 0, y + 10);// 画出字符串
		}

		g.dispose();// 释放画笔
		return image;
	}

}
